<template>
  <div>
    <n-spin :show="loading" description="请稍候...">
      <n-modal
        v-model:show="showModal"
        :mask-closable="false"
        :show-icon="false"
        preset="dialog"
        transform-origin="center"
        :title="formValue.id > 0 ? '编辑 #' + formValue.id : '添加'"
        :style="{
          width: dialogWidth,
        }"
      >
        <n-scrollbar style="max-height: 87vh" class="pr-5">
          <n-form
            :model="formValue"
            :rules="rules"
            ref="formRef"
            :label-placement="settingStore.isMobile ? 'top' : 'left'"
            :label-width="100"
            class="py-4"
          >
            <n-form-item label="涨停家数" path="ztjs">
            <n-input-number placeholder="请输入涨停家数" v-model:value="formValue.ztjs" />
          </n-form-item>

          <n-form-item label="强度" path="strong">
            <n-input-number placeholder="请输入强度" v-model:value="formValue.strong" />
          </n-form-item>

          <n-form-item label="日期" path="date">
            <n-input type="textarea" placeholder="日期" v-model:value="formValue.date" />
          </n-form-item>

          <n-form-item label="连扳高度" path="lbgd">
            <n-input-number placeholder="请输入连扳高度" v-model:value="formValue.lbgd" />
          </n-form-item>

          <n-form-item label="亏钱效应" path="dmNum">
            <n-input-number placeholder="请输入亏钱效应" v-model:value="formValue.dmNum" />
          </n-form-item>

          <n-form-item label="跌停家数" path="dtjs">
            <n-input-number placeholder="请输入跌停家数" v-model:value="formValue.dtjs" />
          </n-form-item>


          </n-form>
        </n-scrollbar>
        <template #action>
          <n-space>
            <n-button @click="closeForm">取消</n-button>
            <n-button type="info" :loading="formBtnLoading" @click="confirmForm">确定</n-button>
          </n-space>
        </template>
      </n-modal>
    </n-spin>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { Edit, View } from '@/api/stockEmotion';
  import { rules, options, State, newState } from './model';
  import { useProjectSettingStore } from '@/store/modules/projectSetting';
  import { useMessage } from 'naive-ui';
  import { adaModalWidth } from '@/utils/hotgo';

  const emit = defineEmits(['reloadTable']);
  const message = useMessage();
  const settingStore = useProjectSettingStore();
  const dialogWidth = ref('75%');
  const loading = ref(false);
  const showModal = ref(false);
  const formValue = ref<State>(newState(null));
  const formRef = ref<any>({});
  const formBtnLoading = ref(false);

  function confirmForm(e) {
    e.preventDefault();
    formBtnLoading.value = true;
    formRef.value.validate((errors) => {
      if (!errors) {
        Edit(formValue.value).then((_res) => {
          message.success('操作成功');
          setTimeout(() => {
            showModal.value = false;
            emit('reloadTable');
          });
        });
      } else {
        message.error('请填写完整信息');
      }
      formBtnLoading.value = false;
    });
  }

  function closeForm() {
    showModal.value = false;
    loading.value = false;
  }

  function openModal(state: State) {
    adaModalWidth(dialogWidth);
    showModal.value = true;
    loading.value = true;

    // 新增
    if (!state || state.id < 1) {
      formValue.value = newState(state);
      return;
    }

    // 编辑
    View({ id: state.id })
      .then((res) => {
        formValue.value = res;
      })
      .finally(() => {
        loading.value = false;
      });
  }

  defineExpose({
    openModal,
  });
</script>

<style lang="less"></style>